define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hd/activity/info/index' + location.search,
                    add_url: 'hd/activity/info/add',
                    edit_url: 'hd/activity/info/edit',
                    del_url: 'hd/activity/info/del',
                    multi_url: 'hd/activity/info/multi',
                    import_url: 'hd/activity/info/import',
                    table: 'hd_activity_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'forms', title: __('Forms'), searchList: { "1": __('志愿活动'), "2": __('投票活动') }, formatter: Table.api.formatter.normal },
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'printer_id',
                                    text: function (row) {
                                        return row.name
                                    },
                                    title: __('活动人员记录'),
                                    classname: 'btn btn-xs btn-dialog',
                                    // classname: 'btn btn-xs btn-addtabs',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'hd/activity/log?act_id={id}&forms={forms}',
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                            // formatter: Table.api.formatter.content 
                        },
                        { field: 'type.name', title: __('Type_id') },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        {
                            field: 'department',
                            title: __('Department_id'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Controller.api.formatter.department,
                        },
                        // {field: 'admin_id', title: __('Admin_id')},
                        // {field: 'reg_start_time', title: __('Reg_start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'reg_end_time', title: __('Reg_end_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'longitude', title: __('Longitude'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'latitude', title: __('Latitude'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        { field: 'status', title: __('Status'), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        details: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                department: function (value, row, index) {

                    if (Array.isArray(value) && value.length > 0) {
                        // 遍历数组，生成每个 `<a>` 标签的 HTML
                        return value
                            .map(item => {
                                const fullText = item.name; // 完整内容
                                const renderedText = fullText.length > 10 ? fullText.slice(0, 10) + '...' : fullText; // 截断内容
                                return `<a class="label" style="color: #000;font-weight: 500; font-size: 12px;" title="${fullText}">${renderedText}</a>`;
                            })
                            .join('<br>'); // 用换行符分隔每行
                    }
                    // 如果 value 为空或不是数组，返回默认值
                    return '-';
                }
            },
        }
    };
    return Controller;
});
